package com.shizhanzhe.zxbapp.task.newshare;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.ShareParams;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;

import org.xml.sax.XMLReader;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * Created by zz9527 on 2019/4/25.
 */
public class InviteFriendActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitefriend);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true).init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final TextView tv = findViewById(R.id.tv);

        final Html.ImageGetter imageGetter = new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {
                Drawable drawable = null;
                int rId = Integer.parseInt(source);
                drawable = getResources().getDrawable(rId);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
        };
        OkHttpDownloadJsonUtil.downloadJson(this, Path.shareParams(), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {

                    Gson gson = new Gson();
                    ShareParams.DataBean data = gson.fromJson(json, ShareParams.class).getData();
                    String text = "<img src=\"" + R.mipmap.invite_1 + "\" /><br><strong><myfont color='black' size='50px'>第一式：只争朝夕</myfont></strong><br>\n" +
                            "        此乃赚钱无上秘笈值基础招式，学会此招，定有事半功倍之效。<br>\n" +
                            "        招式要点：<br>\n" +
                            "        朋友圈高峰时间段：<br>\n" +
                            "        1.早上7:00-7:30(文字类)<br>\n" +
                            "        2.中午12:10-13:10(视频类)<br>\n" +
                            "        3.下午14:00-15:00(文字类)<br>\n" +
                            "        4.傍晚18:30-20:00(视频类或文字类)<br>\n" +
                            "        5.晚上21:00-00:00(视频类)<br><br>\n" +
                            "        <strong><myfont color='black' size='50px'>第二式：点到即止</myfont></strong><br>\n" +
                            "        此招乃秘笈之要点，学成方可循环往复，生生不息。招式要点：文章不宜分享过多，以免朋友圈好友反感，盈满则亏，讲究点到为止，<myfont color='red'>普通用户，每天可以获得分享收益条数上限为 " + data.getSharenum() + " 条;VIP用户，每天可以获得分享收益条数上限为 " + data.getVipsharenum() + " 条</myfont>，效果最好。自己点击自己分享的文章前两篇有收益，后续无收益<br><br>\n" +
                            "        <strong><myfont color='black' size='50px'>第三式：画龙点睛</myfont></strong><br>\n" +
                            "        此招旨在传神授意，吸引好友，激发阅读的欲望。招式要点：在分享文章之前，加入自己的<myfont color='red'>看法和推荐语</myfont>，吸引好友，使其生出阅读之感。<br>\n" +
                            "        <img src=\"" + R.mipmap.invite_2 + "\" /><br><strong><myfont color='black' size='50px'>第四式：仙女散花</myfont></strong><br>\n" +
                            "        此乃秘笈之绝学，需静心踏实，潜心修炼，一人之力终有限，开宗立派，广收门徒，求千人之心，得千人之力，方可财源广进。<br><br>\n" +
                            "        <strong><myfont color='black' size='50px'>招式要点</myfont></strong><br>\n" +
                            "        1)充分发动身边<myfont color='red'>朋友亲戚同事</myfont>来玩赚小宝，可以获得非常不错的收益。<br>\n" +
                            "        2)去论坛贴吧发帖子，用自己的<myfont color='red'>微信，微博</myfont>等社交工具发布<myfont color='red'>收益图和教学</myfont>，教他们用赚小宝赚钱，能收到非常多的高质量徒弟！<br>\n" +
                            "        3)充分动员<myfont color='red' >自己的徒弟去收徒 </myfont>，每个人找10名徒弟，组成一个100人的团队，每个徒弟帮你挣10元，一天就能收益1000元！<br><br>\n" +
                            "        4)对于<myfont color='red' >团队刷量的、利用非法软件模拟操作的、以及各种非正规途径获取收益的坚决予以封号或不予提现；</myfont>对于正常的分享，平台给予最大力度的支持!<br><br>\n"+
                            "        <myfont color='black'>最终解释权归《赚小宝》所有</myfont><br>";
                    tv.setText(Html.fromHtml(text, imageGetter, new HtmlTagHandler("myfont")));
                } catch (Exception e) {
                }
            }
        });


    }


    public class HtmlTagHandler implements Html.TagHandler {
        // 自定义标签名称
        private String tagName;

        // 标签开始索引
        private int startIndex = 0;
        // 标签结束索引
        private int endIndex = 0;
        // 存放标签所有属性键值对
        final HashMap<String, String> attributes = new HashMap<>();

        public HtmlTagHandler(String tagName) {
            this.tagName = tagName;
        }

        @Override
        public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
            // 判断是否是当前需要的tag
            if (tag.equalsIgnoreCase(tagName)) {
                // 解析所有属性值
                parseAttributes(xmlReader);

                if (opening) {
                    startHandleTag(tag, output, xmlReader);
                } else {
                    endEndHandleTag(tag, output, xmlReader);
                }
            }
        }

        public void startHandleTag(String tag, Editable output, XMLReader xmlReader) {
            startIndex = output.length();
        }

        public void endEndHandleTag(String tag, Editable output, XMLReader xmlReader) {
            endIndex = output.length();

            // 获取对应的属性值
            String color = attributes.get("color");
            String size = attributes.get("size");
            size = size.split("px")[0];

            // 设置颜色
            if (!TextUtils.isEmpty(color)) {
                output.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            // 设置字体大小
            if (!TextUtils.isEmpty(size)) {
                output.setSpan(new AbsoluteSizeSpan(Integer.parseInt(size)), startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        /**
         * 解析所有属性值
         *
         * @param xmlReader
         */
        private void parseAttributes(final XMLReader xmlReader) {
            try {
                Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
                elementField.setAccessible(true);
                Object element = elementField.get(xmlReader);
                Field attsField = element.getClass().getDeclaredField("theAtts");
                attsField.setAccessible(true);
                Object atts = attsField.get(element);
                Field dataField = atts.getClass().getDeclaredField("data");
                dataField.setAccessible(true);
                String[] data = (String[]) dataField.get(atts);
                Field lengthField = atts.getClass().getDeclaredField("length");
                lengthField.setAccessible(true);
                int len = (Integer) lengthField.get(atts);

                for (int i = 0; i < len; i++) {
                    attributes.put(data[i * 5 + 1], data[i * 5 + 4]);
                }
            } catch (Exception e) {

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
